//
//  AccountSafeController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/27.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "AccountSafeController.h"
#import "ChangePayforController.h"
#import "ChangePhoneController.h"
#import "ForgetPasswordViewController.h"
#import "PayForPasswordController.h"

@interface AccountSafeController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic , strong) NSArray * titleArray;

@property (nonatomic , strong) UILabel * phoneLabel;
@property (nonatomic , strong) UILabel * passwordLabel;
@property (nonatomic , assign) NSInteger isSetPay;

@end

@implementation AccountSafeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)setNavi
{
    self.title = @"账户安全";
}

-(void)setUI
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"accountCell"];
    self.tableview.tableFooterView = [[UIView alloc]init];
    self.tableview.backgroundColor =  Color(245, 245, 245);
}

- (void)viewWillAppear:(BOOL)animated
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/security",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            self.phoneLabel.text = self.starPhoneNumber = [responseObject objectForKey:@"data"][@"phone_mob"];
            self.isSetPay = [[responseObject objectForKey:@"data"][@"set_payment_password"] integerValue];   // 1 有支付密码  2 未设置
            self.passwordLabel.text = self.isSetPay == 0 ? @"未设置" : @"已设置";
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        
        ChangePhoneController * vc = [[ChangePhoneController alloc]init];
        vc.telePhoneNumString = self.starPhoneNumber;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 1){
        
        ForgetPasswordViewController * vc = [[ForgetPasswordViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else
    {
        if (self.isSetPay) {
            //        2、已经设置 修改支付密码 进入下面的页面
            ChangePayforController * vc = [[ChangePayforController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else
        {
            //        分两种情况 ：1、未设置的话 进入下面的页面
            PayForPasswordController * vc = [[PayForPasswordController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"accountCell" forIndexPath:indexPath];
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        
        [cell.contentView addSubview:self.phoneLabel];
        [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.centerY.equalTo(cell.contentView);
        }];
        
    }else if (indexPath.row == 2)
    {
        [cell.contentView addSubview:self.passwordLabel];
        [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.centerY.equalTo(cell.contentView);
        }];
    }
    
    return cell;
}


-(UILabel *)passwordLabel
{
    if (!_passwordLabel) {
        _passwordLabel = [[UILabel alloc]init];
        _passwordLabel.font = FontSize(14);
        _passwordLabel.textColor = TextLessImportColor;
    }
    return _passwordLabel;
}

-(UILabel *)phoneLabel
{
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc]init];
        _phoneLabel.font = FontSize(14);
        _phoneLabel.textColor = TextAssistColor;
//        NSString * string = [NSString stringWithFormat:@"%@****%@",[self.telephoneNumber substringToIndex:2] , [self.telephoneNumber substringFromIndex:7]];
//        _phoneLabel.text = string;
    }
    return _phoneLabel;
}

-(NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = @[@"手机号码",@"登录密码",@"支付密码"];
    }
    return _titleArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
